capture log close
clear all
set maxvar 10000
*set matsize 11000
set more off
pause off
# delimit;

local crime "";
local data "";

/* 
Sample of SSI children 1990-2012, prep 2
*/


/* PO/drug/property breakout variables */
local abbr_all "bk" ;
local abbr_non_income 	"nobk" ;

foreach type in all non_income { ;
	use "`crime'\reference\reference_event_`type'_breakout.dta", clear ;
	foreach var of varlist _all { ;
		rename `var' `abbr_`type''_`var' ;
	} ;
	rename `abbr_`type''_cjars_id cjars_id ;
	tempfile `abbr_`type'' ;
	save ``abbr_`type''' ;
} ;


/* Merge in the linked crime records */
use "`crime'\831\crime_allssr_formerge_prep.dta", clear ;

duplicates drop hun, force;
merge 1:m hun using "`crime'\prep1\crime_allssr_merged.dta", keepusing(part cjars_id);
gen byte indiv_match=(_merge==3);
drop if _merge==2;
drop _merge;
count;

tab indiv_match part, m;
pause ;

/* ****************************** */
/* **** ELIMINATE DUPLICATES **** */
/* ****************************** */

/* Most duplicates come from multiple "parts" in 1merge, rest are mostly duplicates in all variables */
duplicates report hun;
duplicates report hun part;

/* Drop parts */
tab part, m ;
forval i = 1(1)2 {;
	gen byte temp`i' = (part == `i') ;
	egen part`i' = max(temp`i'), by(hun) ;
	tab part`i', m ;
};
drop temp* ;

drop if part1==1 & part!=1 ;
drop if part1==0 & part2==1 & part!=2 ;
tab part, m ;
drop part1 part2 ;

/* Verify only one part by adding in "part" to duplicates and seeing that it doesn't change anything */
duplicates report hun ;
duplicates report hun part ;

sort hun ;
duplicates report hun ;
duplicates drop hun, force ;

tab indiv_match, m ;
pause ;
	
/* ********************************************* */
/* **** MERGE IN EVENTS FROM REFERENCE FILE **** */
/* ********************************************* */

/* Merge reference file */
merge m:1 cjars_id using "`crime'\reference\reference_event.dta" ;
drop if _merge == 2 ;
drop _merge ;

/* Merge reference file for breakouts */
foreach type in bk nobk { ;
	merge m:1 cjars_id using ``type'' ;
	drop if _merge == 2 ;
	drop _merge ;
} ;


/* Merge incarceration spells */
merge m:1 cjars_id using "`crime'\reference\incarceration_annual_1997_2019_flat.dta", keepusing(days_in_*) ;
gen byte indiv_match_inc=(_merge==3);
drop if _merge == 2 ;
drop _merge ;

merge m:1 cjars_id using "`crime'\reference\parole_annual_1997_2019_flat.dta", keepusing(days_in_*) ;
gen byte indiv_match_par=(_merge==3);
drop if _merge == 2 ;
drop _merge ;

merge m:1 cjars_id using "`crime'\reference\probation_annual_1997_2019_flat.dta", keepusing(days_in_*) ;
gen byte indiv_match_pro=(_merge==3);
drop if _merge == 2 ;
drop _merge ;

foreach var of varlist days_in_* { ;
	replace `var' = 0 if `var' == . ;
} ;

/* Save data set */
save "`crime'\prep2\crime_allssr_remerge.dta", replace;

capture log close;
